package com.yunhe.sms.dao.system.impl;

import com.yunhe.commons.dao.impl.BaseDaoImpl;
import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.JdbcUtils;
import com.yunhe.sms.dao.system.PermissionDao;
import com.yunhe.sms.entity.system.Permission;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19 0019.
 */
public class PermissionDaoImpl extends BaseDaoImpl<Permission> implements PermissionDao{
    @Override
    public void save(Permission permission) {
        String sql = " insert into sys_permission(name,uri,type,parent_id,id_name,sort_no,created_time,deleted) values(?,?,?,?,?,?,now(),0) ";
        super.cud(sql,permission.getName(),permission.getUri(),permission.getType(),permission.getParentId(),permission.getIdName(),permission.getSortNo());
    }

    @Override
    public void delete(Long id) {
        String sql = " update sys_permission set deleted=1 where id = ? ";
        super.cud(sql,id);
    }

    @Override
    public void update(Permission permission) {
        String sql = " update sys_permission set name = ?, uri = ?,type = ?,parent_id = ?,sort_no = ? ,id_name = ? where id = ? ";
        super.cud(sql,permission.getName(),permission.getUri(),permission.getType(),permission.getParentId(),permission.getSortNo(),permission.getIdName(),permission.getId());
    }

    @Override
    public Permission find(Long id) {
        String sql = " select id,name,uri,type,parent_id parentId,sort_no sortNo,id_name idName,created_time createdTime from sys_permission where deleted = 0 and  id = ? ";
        Permission permission = super.queryOne(sql, id);
        return permission;
    }

    @Override
    public List<Permission> findAll() {
        String sql = " select id,name,uri,type,parent_id parentId,sort_no sortNo,id_name idName,created_time createdTime from sys_permission where deleted = 0  order by type asc";
        List<Permission> permissions = super.queryList(sql);
        return permissions;
    }

    @Override
    public List<Permission> findByUser(Long userId) {
        String sql = " select p.id,p.name,p.uri,p.type,p.parent_id parentId ,p.sort_no sortId,p.created_time createdTime from sys_permission p,sys_user u,sys_role_permission rp where p.deleted = 0 and u.deleted = 0 and p.id=rp.permission_id and u.role_id=rp.role_id and u.id=? and p.possition=1 ";
        List<Permission> permissions = super.queryList(sql,userId);
        return permissions;
    }

    @Override
    public List<Long> findIdListByRole(Long roleId) {
        String sql = " select p.id  from sys_permission p,sys_role r,sys_role_permission rp where p.deleted = 0 and r.deleted = 0 and p.id=rp.permission_id and r.id=rp.role_id and r.id=? ";
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        List<Long> idList = null;
        try {
             idList = qr.query(sql, new ColumnListHandler<Long>(), roleId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return idList;

    }

    @Override
    public List<Permission> findByRole(Long roleId) {
        String sql = " select p.id,p.name,p.uri,p.type,p.parent_id parentId ,p.sort_no sortId,p.created_time createdTime  from sys_permission p,sys_role r,sys_role_permission rp where p.deleted = 0 and r.deleted = 0 and p.id=rp.permission_id and r.id=rp.role_id and r.id=? ";
        List<Permission> permissions = super.queryList(sql, roleId);
        return permissions;
    }

    @Override
    public List<Permission> findPermiMenuByRole(Long roleId, Integer type) {
        String sql = " select p.id,p.name,p.uri,p.type,p.parent_id parentId ,p.sort_no sortId,p.created_time createdTime  from sys_permission p,sys_role r,sys_role_permission rp where p.deleted = 0 and r.deleted = 0 and p.id=rp.permission_id and r.id=rp.role_id and r.id=? and p.type = ? ";
        List<Permission> permissionList = super.queryList(sql, roleId, type);
        return permissionList;
    }

    @Override
    public Pageable<Permission> findPage(Integer offset, Integer size) {
          String listSql = " select id,name,uri,type,parent_id parentId,sort_no sortNo,created_time createdTime from sys_permission  where deleted = 0 and type in (1,9)  ";
          String countSql = " select count(*) from sys_permission where deleted = 0 and type in (1,9) ";
          Pageable<Permission> pageable = super.queryPage(listSql, countSql, offset, size);
          return pageable;
    }

    @Override
    public List<Permission> findListByParent(Long parentId) {
        String sql = " select id,name,uri,type,parent_id parentId,sort_no sortNo,id_name idName,created_time createdTime from sys_permission  where deleted = 0 and parent_id = ?  order by sort_no asc ";
        List<Permission> permissionList = super.queryList(sql, parentId);
        return permissionList;
    }

    @Override
    public List<Permission> finfListByType(Integer type) {

        String sql = " select id,name,uri,type,parent_id parentId,sort_no sortNo,id_name idName,created_time createdTime from sys_permission where deleted = 0  and type = ? ";
        List<Permission> permissions = super.queryList(sql, type);
        return permissions;
    }

    @Override
    public List<Permission> findButtons(Long parentId) {
        String sql = " select id,name,uri,type,parent_id parentId,sort_no sortNo,id_name idName,created_time createdTime from sys_permission where deleted = 0  and type = 3 and parent_id = ? ";
        List<Permission> permissions = super.queryList(sql, parentId);

        return permissions;
    }
}
